<?php
/**
 * Slipstream :: Highly adaptive dependency injection framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Slipstream\Infrastructure\Context\Relevance;

use Slipstream\Infrastructure\Context\Type\ResolverServiceInterface;
use Slipstream\Configuration\ContextInterface;

/**
 * Analyzer that determines if a context's identifier is a partial match
 * to the resolved type of the target context
 *
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
class PartialTypeAnalyzer implements AnalyzerInterface
{
	const WEIGHT_PATTERN_MATCH = 300;
	const WEIGHT_NAMESPACE_DEPTH_BONUS = 10;
	
	/**
	 * @var ResolverServiceInterface
	 */
	protected $typeResolver;
	
	/**
	 * Class constructor
	 *
	 * @param ResolverServiceInterface $typeResolver
	 * @return void
	 */
	public function __construct(ResolverServiceInterface $typeResolver)
	{
		$this->typeResolver = $typeResolver;
	}
	
	/**
	 * Returns the relevance weight of a given context in terms of the target
	 * context
	 *
	 * @param ContextInterface $context
	 * @param ContextInterface $targetContext
	 * @return int|false Returns false if relevance can't be determined
	 */
	public function getWeight(ContextInterface $context, ContextInterface $targetContext)
	{
		$identifier = $context->getIdentifier();
		if(strpos($identifier, '*') === false) {
			return false;
		}
		
		$resolvedTargetType = $this->typeResolver->resolveType($targetContext);
		if(!$resolvedTargetType) {
			// Only supports target contexts with a resolved type
			return false;
		}
		
		$pattern = $this->formatRegexPattern($identifier);
		
		if(preg_match($pattern, $resolvedTargetType)) {
			$weight = self::WEIGHT_PATTERN_MATCH;
			$weight += $this->calculateDepthBonus($identifier);
			
			return $weight;
		}
		return 0;
	}
	
	/**
	 * Calculates the weight to be added because of the depth of the namespace
	 *
	 * @param string $identifier
	 * @return int
	 */
	protected function calculateDepthBonus($identifier)
	{
		$separator = (strpos($identifier, '\\') !== false ? '\\' : '_');
		$depth = count(explode($separator, $identifier));
		return ($depth * self::WEIGHT_NAMESPACE_DEPTH_BONUS);
	}
	
	/**
	 * Formats a partial type identifier into a regular expression pattern
	 *
	 * @param string $identifier
	 * @return string
	 */
	protected function formatRegexPattern($identifier)
	{
		$pattern = str_replace('\\', '\\\\', $identifier);
		$pattern = str_replace('*', '([\w\d]*)', $pattern);
		
		return sprintf('/%s/i', $pattern);
	}
}